package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Accdentdetail;
import cn.wolfcode.crm.domain.Claim;
import cn.wolfcode.crm.query.CalimQueryObject;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IAccdentdetailService;
import cn.wolfcode.crm.service.IClaimService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("claim")
public class ClaimController {

    @Autowired
    private IClaimService claimService;
    @Autowired
    private IAccdentdetailService accdentdetailService;

    @RequestMapping("view")
    @RequiresPermissions(value = {"claim:view","清单页面"},logical = Logical.OR)
    public String view() throws Exception{

        return "claim/claim";
    }

    @RequestMapping("selectAll")
    @ResponseBody
    public List<Claim> selectAll() throws Exception{
        return claimService.selectAll();
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value = {"claim:saveOrUpdate","清单新增/修改"},logical = Logical.OR)
    public JsonResult saveOrUpdate(Claim entity) throws Exception{
        JsonResult jsonResult = new JsonResult();
        try{
            claimService.saveOrUpdate(entity );
        }catch (Exception e){
            jsonResult.mark("操作失败");
        }
        return jsonResult;
    }
    //修改或添加claim
    @RequestMapping("updateDetail")
    @ResponseBody
    @RequiresPermissions(value = {"claim:updateDetail","清单新增/修改"},logical = Logical.OR)
    public JsonResult updateDetail(Accdentdetail entity) throws Exception{
        JsonResult jsonResult = new JsonResult();
        Claim claim = new Claim();
        claim.setId(entity.getClaim().getId());
        claim.setAmount(entity.getClaim().getAmount());
        claim.setCardnumber(entity.getClaim().getCardnumber());
        claim.setClient(entity.getClaim().getClient());
        claim.setDamagcondition(entity.getClaim().getDamagcondition());
        claim.setGuardNum(entity.getClaim().getGuardNum());
        claim.setGuardNum(entity.getGuardNum());
        try{
            //修改claim_id
            accdentdetailService.updateClaim(entity.getId());
            claimService.saveOrUpdate(claim);
        }catch (Exception e){
            jsonResult.mark("操作失败");
        }
        return jsonResult;
    }



    @RequestMapping("list")
    @ResponseBody
    public PageResult list(CalimQueryObject qo) throws Exception{
        return claimService.query(qo);
    }

}
